package com.aurora.oa.work.presenter;

import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnSendLogListener;
import com.aurora.oa.listener.OnUploadAttachmentListener;
import com.aurora.oa.message.bean.ResponseFileID;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.StringUtils;
import com.aurora.oa.work.bean.SendLogBean;
import com.aurora.oa.work.view.WriteLogDetailActivityView;
import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
* Created by Administrator on 2017/03/30
*/

public class WriteLogDetailActivityPresenterImpl implements WriteLogDetailActivityPresenter{

    WriteLogDetailActivityView view;
    RequestHttpModel model;
    List<File> files;
    List<String> fileIDs;
    SendLogBean logData;
    public WriteLogDetailActivityPresenterImpl(WriteLogDetailActivityView writeLogDetailActivity) {
        view = writeLogDetailActivity;
        model = new RequestHttpModelImpl();
    }

    @Override
    public void sendLog(){
        //获取要发送的数据
        logData= view.getLogData();
        //为空就不发送消息
        if(checkLogNull()){
            return;
        }

        view.showProgress();
        files = view.getImageFiles();
        if(files.size()==0){
            //发送没有图片的数据
            model.sendWorkLog(logData, new OnSendLogListener() {
                @Override
                public void onSendLogSuccess(HttpResult body) {
                    view.onLogSendSuccess();
                }

                @Override
                public void onSendLogFail(String message) {
                    view.onLogSendFail(message);
                }
            });
            return;
        }
        fileIDs = new ArrayList<>();
        //先循环发送图片
        for(int i = 0 ;i<files.size();i++){
            model.uploadAttachment(files.get(i), new OnUploadAttachmentListener() {
                @Override
                public void onUploadAttachmentSuccess(ResponseFileID attachId) {
                    fileIDs.add(attachId.getMsg());
                    //所有图片上传完成
                    if(fileIDs.size()==files.size()){
                        //开始上传数据
                        logData.attachmentids = StringUtils.getStringFromList(fileIDs);
                        model.sendWorkLog(logData, new OnSendLogListener() {
                            @Override
                            public void onSendLogSuccess(HttpResult body) {
                                view.onLogSendSuccess();
                            }

                            @Override
                            public void onSendLogFail(String message) {
                                view.onLogSendFail(message);
                            }
                        });
                    }

                }

                @Override
                public void onUploadAttachmentFail(String message) {
                    view.onLogSendFail(message);
                }
            });
        }

    }

    //非空校验
    private boolean checkLogNull() {

        if(StringUtils.isNullString(logData.completed)){
            view.onLogSendFail("已完成不能为空");
            return true;
        }
        return false;
    }
}